package com.example.pei.ithinklearnt.view.customview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anenn.flowlikeviewlib.FlowLikeView;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.NineGridTestAdapter;
import com.example.pei.ithinklearnt.view.activity.FrescoLoaderPicturesActivity;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.displayImage;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption2;


public class DynamicDetailsHeaderView extends LinearLayout implements View.OnLongClickListener,View.OnClickListener{

    private Context context;
    private NineGridImageView<String> mNglContent;
    private TextView textView_sendName;
    private JustifyTextView textView_sendContent;
    private TextView textView_sendTime;
    private RoundedImageView roundedImageView_head;
    private ImageView imageView_sex;
    private TextView textView_heart;
    private TextView textView_praiseNum;
    private TextView textView_commentNum;
    private TextView textView_commentNum2;
    private FlowLikeView likeViewLayout;

    private NineGridTestAdapter.itemListener mItemListener;

    public DynamicDetailsHeaderView(Context context,NineGridTestAdapter.itemListener listener) {
        super(context);

        this.context=context;
        this.mItemListener=listener;

        View view = LayoutInflater.from(this.context).inflate(R.layout.dynamic_details_header, null);
        //以下两句的顺序不能调换，要先addView，然后才能通过findViewById找到该TextView
        addView(view);

        NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, String s) {
                displayImage(context, imageView, s, getPhotoImageOption());
            }

            @Override
            protected ImageView generateImageView(Context context) {
                return super.generateImageView(context);
            }

            @Override
            protected void onItemImageClick(Context context, ImageView imageView, int index, List<String> list) {
                ArrayList<String> arrayList = (ArrayList<String>) list;
                Intent intent=new Intent(context,FrescoLoaderPicturesActivity.class);
                intent.putStringArrayListExtra("photoUris",arrayList);
                intent.putExtra("currentPage",index);
                context.startActivity(intent);

            }

            @Override
            protected boolean onItemImageLongClick(Context context, ImageView imageView, int index, List<String> list) {
                //  Toast.makeText(context, "image long click position is " + index, Toast.LENGTH_SHORT).show();
                return true;
            }
        };

        mNglContent = view.findViewById(R.id.ngl_images);
        mNglContent.setAdapter(mAdapter);

        textView_sendName=view.findViewById(R.id.tv_sendname);
        textView_sendContent=view.findViewById(R.id.tv_sendcontent);
        textView_sendTime=view.findViewById(R.id.tv_sendtime);
        roundedImageView_head=view.findViewById(R.id.img_roudhead);
        imageView_sex=view.findViewById(R.id.iv_sex);

        textView_heart= view.findViewById(R.id.tv_heart);
        textView_praiseNum=view.findViewById(R.id.tv_praisenum);
        textView_commentNum=view.findViewById(R.id.tv_commentnum);
        textView_commentNum2=view.findViewById(R.id.tv_commentnum2);
        likeViewLayout = view.findViewById(R.id.flowLikeView);

        //设置点击喜欢时的特效 以及点赞计数
        textView_heart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mItemListener.onHeartClick(view,999);
                likeViewLayout.addLikeView();
            }
        });
        roundedImageView_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemListener.onHeadClick(v,999);
            }
        });
        view.setOnLongClickListener(this);   //设置长按监听
        view.setOnClickListener(this);
    }
    @Override
    public boolean onLongClick(View view) {
        mItemListener.onItemLongClick(view,0);
        return false;
    }

    @Override
    public void onClick(View view) {
        mItemListener.onItemClick(view,0);
    }

    //设置九宫格图片的资源
    public void setImagesData(ArrayList<String> lists, int spanType){
        mNglContent.setImagesData(lists,spanType);
    }
    //设置动态发送者的名称
    public void setSenderName(String name){
        textView_sendName.setText(name);
    }
    //设置动态发送时间
    public void setSendTime(String time){
        textView_sendTime.setText(time);
    }
    //设置动态的正文，文字部分
    public void setSendContent(String content){
        if(content==null||content.equals("")){
            textView_sendContent.setVisibility(GONE);
        }else{
            textView_sendContent.setText(content);
            textView_sendContent.setVisibility(VISIBLE);
        }

    }
    //设置发送者的头像
    public void setSenderHead(String headUrl){
//        roundedImageView_head.setImageBitmap(bitmap);
        displayImage(getContext(),roundedImageView_head,headUrl,getPhotoImageOption2());
    }
    //设置发送者的性别
    public void setSenderSex(int sex){
        if(sex==1){
            imageView_sex.setBackgroundResource(R.drawable.male);
        }else {
            imageView_sex.setBackgroundResource(R.drawable.female);
        }
    }
    //设置被点赞的个数
    public void setPraiseNum(int num){
        if(num<999){
            textView_praiseNum.setText(num+"喜欢  ");
        }else {
            textView_praiseNum.setText("999+    ");
        }
    }
    //设置评论个数
    public void setCommentNum(int num){
        textView_commentNum.setText("  "+num+"评论");
        textView_commentNum2.setText("评论 "+ num+" 条");
    }


    //设置用户是否点赞过这个动态
    public void setIsPraised(int praised){
        if(praised==0){
            textView_heart.setTextColor(Color.parseColor("#b1c6d7"));
            textView_heart.setTextSize(15);
            textView_praiseNum.setTextColor(Color.parseColor("#828282"));
        }else {
            textView_heart.setTextColor(Color.parseColor("#FC81AF"));
            textView_heart.setTextSize(18);
            textView_praiseNum.setTextColor(Color.parseColor("#FC81AF"));
        }
    }
    public TextView getTextView_praiseNum(){return textView_praiseNum;}
    public FlowLikeView getLikeViewLayout(){return likeViewLayout;}
}
